﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RestFullChatAJAX.Models;
using System.Threading;
using System.IO;
using Newtonsoft.Json.Linq;

namespace RestFullChatAJAX.Controllers
{
    public class YahooStateController : ApiController
    {
        private const String URL_OAUTH_NOTIFICATION = "http://localhost:53220/api/OAuthMessage";

        // GET api/YahooState/username
        public List<NotifyInfo> GetState(String id)
        {
            List<NotifyInfo> lst = new List<NotifyInfo>();
            int index = IndexManager.FindYahooIndex(id.Trim());
            if (index == -1)
                return null;
            do
            {
                //Interval check notify
                Thread.Sleep(2000);
                index = IndexManager.FindYahooIndex(id);
                if (index == -1)
                    return null;
                lst = GetNotifyYahoo(index);
            } while (lst.Count == 0);
            return lst;
        }

        private List<NotifyInfo> GetNotifyYahoo(int index)
        {
            List<NotifyInfo> notify = new List<NotifyInfo>();
            if (index != -1)
            {
                try
                {
                    YahooInfo yi = IndexManager.GetYahooInfo(index);
                    int lastSeq = yi.LastSeq;
                    String urlrequest = URL_OAUTH_NOTIFICATION + yi.Oauth.getHeader(IndexManager.CONSUMERKEY, IndexManager.CONSUMERKEYSECRECT);
                    HttpWebRequest Request = HttpWebRequest.Create(urlrequest.Trim()) as HttpWebRequest;
                    Request.Method = "GET";

                    using (HttpWebResponse response = Request.GetResponse() as HttpWebResponse)
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            // Create Stream Reader                   
                            StreamReader reader = new StreamReader(response.GetResponseStream());
                            // Return                    
                            String resp = reader.ReadToEnd();
                            resp = resp.Substring(1, resp.Length - 2);
                            String[] mess = resp.Split(',');
                            if (mess.Length > 0)
                            {
                                foreach (String i in mess)
                                {
                                    String temp = i.Substring(1, i.Length - 2);
                                    String[] o = temp.Split('@');
                                    NotifyInfo info = new NotifyInfo();
                                    if (o[0] == null || yi.Username == null || o[1] == null)
                                        continue;
                                    info.Actor = String.Format("{0},{1},{2}", (String)o[0], (String)yi.Username, (String)o[1]);
                                    info.Type = "Message";
                                    notify.Add(info);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    return notify;
                }
            }
            return notify;
        }
    }
}
